<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 
            猜数字 
            猜对数字和位置的 - A  x为A的个数
            猜对数字但位置不对的 - B  y为A的个数
            eg.
            "1807" "7810"  - "1A3B"
            "1123" "0111"  - "1A1B"
        */
        /*                 let secret = "1807";
                        let guess = "7810"; */

        function getHint(secret, guess) {
            let arr_s = secret.split("");
            let arr_g = guess.split("");
            let count_A = 0, count_B = 0;
            for (let i = 0; i < arr_s.length; i++) {
                if (arr_s[i] == arr_g[i]) {
                    count_A++;
                }
            }
            for (let i = 0; i < arr_s.length; i++) {
                let judge = false;
                for (let j = 0; j < arr_s.length; j++) {
                    let flag = false;
                    if (j > 0) {
                        for (let k = j - 1; k >= 0; k--) {
                            if (arr_g[k] == arr_g[j]) {
                                flag = true;
                            }
                        }
                    }
                    if (flag)
                        break;
                    if ((arr_g[i] == arr_s[j]) && (arr_s[i] != arr_g[i])) {
                        judge = true;
                        console.log(arr_g[j]);
                    }
                }
                if (judge) {
                    count_B++;
                }
            }
            return (count_A + "A" + count_B + "B");
        }

        console.log(getHint("1000", "0001"));
    </script>
</body>

</html>